package blueBridge;

import java.math.BigInteger;

public class LuckyNumber {

    public static void main(String[] args) {
        int i = 1, count = 0;
        while (true) {
            String bin = Integer.toBinaryString(i);
            String oct = Integer.toOctalString(i);
            String ten = String.valueOf(i);
            String hex = Integer.toHexString(i);
            // System.out.println(bin + ", " + oct + ", " + ten + ", " + hex);
            
            Integer binSum = sum(bin, 2);
            Integer octSum = sum(oct, 8);
            Integer tenSum = sum(ten, 10);
            Integer hexSum = sum(hex, 16);
            // System.out.println(binSum + ", " + octSum + ", " + tenSum + ", " + hexSum);
            
            // System.out.println(i + ", " + count);
            
            if (i % binSum == 0 && i % octSum == 0 && i % tenSum == 0 && i % hexSum == 0) {
                count++;
            }
            if (count == 2023) {
                break;
            }
            i++;
        }
        System.out.println(i);
    }

    private static int sum(String str, int radix) {
        BigInteger sum = new BigInteger("0", radix);
        for (int i = 0; i < str.length(); i++) {
            // sum.add(...)不会改变sum的值，必须重新赋值
            sum = sum.add(new BigInteger(str.substring(i, i+1), radix));
        }
        return sum.intValue();
    }

}
